<?php

use app\modules\Admin\models\memberwalletrechargeModel;
use Illuminate\Database\Capsule\Manager as DB;
#[用户充值记录管理]
class WalletrechargeController extends AcommonController
{
    public function init() {
        $this->model = new memberwalletrechargeModel();
        $this->table = 'member_wallet_recharge';
        $this->primaryKey = 'id';
        parent::init();
    }

    #[用户充值记录列表]
    public function indexAction($returnData = FALSE)
    {
        $this->model = $this->model->with(['getMember']);
        if($this->isPost()){
            $post_data = $this->postData;
            $search_data = [];
            $StatusArray = [
                'success' => 1,
                'waiting' => 0,
            ];
            if(isset($post_data['status']) && !empty($post_data['status']) && in_array($post_data['status'], array_keys($StatusArray))){
                array_push($search_data, ['status', '=', $StatusArray[$post_data['status']]]);
            }
            if(isset($post_data['recharge_start']) && !empty($post_data['recharge_start'])){
                $this->model = $this->model->whereDate('created_at', '>=', $post_data['recharge_start']);
            }
            if(isset($post_data['recharge_end']) && !empty($post_data['recharge_end'])){
                $this->model = $this->model->whereDate('created_at', '<=', $post_data['recharge_end']);
            }
            $keywordsTypeArray = [
                'user_id' => 'user_id',
                'user_phone' => 'user_phone',
            ];
            if(isset($post_data['keywordsType']) && isset($post_data['keywords'])
                && in_array($post_data['keywordsType'], $keywordsTypeArray)
                && !empty($post_data['keywords'])){
                if($post_data['keywordsType'] == $keywordsTypeArray['user_id'] && intval($post_data['keywords']) > 0){
                    array_push($search_data, ['member_id', '=', $post_data['keywords']]);
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['user_phone']){
                    if(mb_strlen($post_data['keywords'], 'utf-8') < 4){
                        array_push($search_data, ['id', '=', 0]);
                    }else{
                        array_push($this->condition_in, ['member_id', memberModel::query()->where('phone', 'like', "%{$post_data['keywords']}%")->pluck('id')->toArray()]);
                    }
                }
            }
            unset($keywordsTypeArray);
            $this->search = $search_data;
        }
        return parent::indexAction($returnData); // TODO: Change the autogenerated stub
    }

    #[充值记录导出]
    public function exportAction()
    {
        $type     = $this->get('keywordsType');
        $keywords = $this->get('keywords');
        $startOn = $this->get('start_on');
        $endOn = $this->get('end_on');
        $mode = $this->get('mode');

        $dirpath = APP_PATH . '/public/excel/';
        if ($dirpath !== '.' && !file_exists($dirpath)) {
            mkdir($dirpath, 0777, true);
        }
        $filename = 'WalletRecharge' . date('Ymd') . uniqid() . '.xlsx';
        $filepath = $dirpath . $filename;
        $res = exec("php ". APP_PATH . "/public/cli.php export/walletrecharge \"mode={$mode}&type={$type}&keywords={$keywords}&start_on={$startOn}&end_on={$endOn}&path={$filepath}\"");
        if ($res === 'Done' && file_exists($dirpath . $filename)) {
			if( $webpath = syncFile($filepath) ){
                ret(0, '充值记录导出excel成功', $webpath);
            }
        }
        ret(30003, '充值记录导出失败');
    }
}